class TestCase:
    def __init__(
        self, cid, module, title, priority, condition, step, expected, actual, bug_id
    ):
        self.cid = cid
        self.module = module
        self.title = title
        self.priority = priority
        self.condition = condition
        self.step = step
        self.expected = expected
        self.actual = actual
        self.bug_id = bug_id

    def __str__(self):
        return f"用例编号：{self.cid}\t功能模块：{self.module}\t测试标题：{self.title}\t优先级别：{self.priority}\t前置条件：{self.condition}\t测试步骤：{self.step}\t预期结果：{self.expected}\t实际结果：{self.actual}\t关联Bug的ID：{self.bug_id}"


class Bug:
    def __init__(self, bid, description, status):
        self.bid = bid
        self.description = description
        self.status = status

    def __str__(self):
        return (
            f"Bug编号：{self.bid}\tBug描述：{self.description}\tBug状态：{self.status}"
        )


class BugManager:
    def __init__(self, bugs):
        self.bugs = bugs

    def list_bugs(self):
        return self.bugs

    def add_bug(self, bug):
        self.bugs.append(bug)

    def get_bug(self, bid):
        for bug in self.bugs:
            if bug.bid == bid:
                return bug
        return None

    def update_bug(self, bid, bug):
        for i, b in enumerate(self.bugs):
            if b.bid == bid:
                self.bugs[i] = bug
                return True
        return False

    def delete_bug(self, bid):
        for i, bug in enumerate(self.bugs):
            if bug.bid == bid:
                del self.bugs[i]
                return True
        return False


class TestcaseManager:
    def __init__(self, testcases):
        self.testcases = testcases

    def list_testcases(self):
        return self.testcases

    def add_testcase(self, testcase):
        self.testcases.append(testcase)

    def get_testcase(self, cid):
        for testcase in self.testcases:
            if testcase.cid == cid:
                return testcase
        return None

    def update_testcase(self, cid, testcase):
        for i, t in enumerate(self.testcases):
            if t.cid == cid:
                self.testcases[i] = testcase
                return True
        return False

    def delete_testcase(self, cid):
        for i, testcase in enumerate(self.testcases):
            if testcase.cid == cid:
                del self.testcases[i]
                return True
        return False

    def search_testcases(self, module=None, priority=None, bug_id=None):
        results = []
        for testcase in self.testcases:
            if (
                (module is None or testcase.module == module)
                and (priority is None or testcase.priority == priority)
                and (bug_id is None or testcase.bug_id == bug_id)
            ):
                results.append(testcase)
        return results


def ShowList(l: list):
    for i in l:
        print(i)


# 通过用例管理类的实例对象调用各个方法对管理类中的方法功能进行测试。
if __name__ == "__main__":
    bugs = [
        Bug("1", "登录功能异常", "未解决"),
        Bug("2", "注册功能异常", "已解决"),
    ]
    testcases = [
        TestCase("1", "登录功能测试", "登录功能", "高", None, "1", False, False, "1"),
        TestCase("2", "注册功能测试", "注册功能", "中", None, "2", False, False, "2"),
    ]
    bug_manager = BugManager(bugs)
    testcase_manager = TestcaseManager(testcases)
    # 测试用例管理类
    ShowList(testcase_manager.list_testcases())

    testcase_manager.add_testcase(
        TestCase("3", "测试用例3", "模块3", "低", None, "1", False, False, "1")
    )
    ShowList(testcase_manager.list_testcases())
    print(testcase_manager.get_testcase("2"))
    print(
        testcase_manager.update_testcase(
            "2", TestCase("2", "测试用例2", "模块2", "高", None, "1", False, False, "2")
        )
    )
    print(testcase_manager.delete_testcase("1"))
    print(testcase_manager.search_testcases(module="模块3", priority="低"))

    # 缺陷管理类
    bug_manager.add_bug(Bug("3", "缺陷3", "未解决"))
    ShowList(bug_manager.list_bugs())
    print(bug_manager.get_bug("2"))
    print(bug_manager.update_bug("2", Bug("2", "缺陷2", "已解决")))
    print(bug_manager.delete_bug("1"))
